﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using GraphDrawer.GraphProvider;
using System.Windows.Data;
using Visifire.Charts;
using Visifire.Commons;

namespace GraphDrawer {
	public partial class SimulationReportChart : UserControl {
		String token = "12";
		private GraphProvider.GraphProviderServiceSoapClient cl;
		public Chart chart;

		public SimulationReportChart(GraphProvider.GraphProviderServiceSoapClient cl) {
			InitializeComponent();
			
			this.cl = cl;;
			cl.getSimulationReportCompleted += new EventHandler<GraphProvider.getSimulationReportCompletedEventArgs>(cl_getSimulationReportCompleted);
			LayoutRoot.SizeChanged += new SizeChangedEventHandler(LayoutRoot_SizeChanged);
		}

		public void Init() {
			cl.getSimulationReportAsync(token);
		}

		private void cl_getSimulationReportCompleted(object sender, GraphProvider.getSimulationReportCompletedEventArgs e) {
			SimulationReport report = e.Result;

			chart = new Chart();
			chart.Width = LayoutRoot.Width;
			chart.Height = LayoutRoot.Height;
			chart.Background = new SolidColorBrush(Colors.Gray);

			foreach (var dataSet in report.dataSets) {
				DataSeries dataSeries = new DataSeries();
				dataSeries.LabelText = dataSet.label;
				dataSeries.LegendText = dataSet.label;
				dataSeries.RenderAs = RenderAs.Column;
				dataSeries.Color = new SolidColorBrush((Color)(typeof(Colors)).GetProperty(dataSet.color).GetValue(null, null));
				for (int i = 0; i < dataSet.values.Count; i++) {
					DataPoint dataPoint = new DataPoint();
					dataPoint.YValue = dataSet.values[i];
					dataPoint.AxisXLabel = i.ToString();
					dataSeries.DataPoints.Add(dataPoint);
				}
				chart.Series.Add(dataSeries);
			}
			LayoutRoot.Children.Add(chart);
		}

		void LayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e) {
			if (chart != null) {
				chart.Width = e.NewSize.Width;
				chart.Height = e.NewSize.Height;
			}
		}
	}
}
